using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Baz
{
    public class AnimatedSprite : Sprite
    {
        public Dictionary<string, Animation> Animations =
                                    new Dictionary<string, Animation>();
        string _current_animation;
        bool _update_animation = true;

        /// <summary>
        /// Getters and setters for _current_animation
        /// </summary>
        public string CurrentAnimation
        {
            get { return _current_animation; }
            set
            {
                if (!Animations.ContainsKey(value))
                    throw new Exception("Invalid animation specified.");

                if (_current_animation == null || !_current_animation.Equals(value))
                {
                    _current_animation = value;
                    Animations[_current_animation].Reset();
                }
            }
        }

        /// <summary>
        /// Sets the _update_animation to true so animations will
        /// continue
        /// </summary>
        public void StartAnimation()
        {
            _update_animation = true;
        }
        /// <summary>
        /// Sets the _update_animation to false so animatios will
        /// halt.
        /// </summary>
        public void StopAnimation()
        {
            _update_animation = false;
        }

        public override void Update(GameTime gameTime)
        {
            if (_current_animation == null)
            {
                if (Animations.Keys.Count == 0)
                    return;

                string[] keys = new string[Animations.Keys.Count];

                Animations.Keys.CopyTo(keys, 0);
                _current_animation = keys[0];
            }

            if (_update_animation)
                Animations[_current_animation].Update(gameTime);

            base.Update(gameTime);
        }

        public override void Draw(SpriteBatch batch)
        {
            batch.Draw( _texture, Position,Animations[_current_animation].CurrentFrame, Color.White);
        }
    }
}
